home *** CD-ROM | disk | FTP | other *** search
- /*
-
- GeniusBar
-
- Joshua Juran
-
- */
-
- #include <string.h>
-
- #include <Controls.h>
- #include <ControlDefinitions.h>
- #include <Events.h>
- #include <MacWindows.h>
- #include <Processes.h>
- #include <QuickDraw.h>
- #include <Resources.h>
-
- // Needed for globals
- #include <A4Stuff.h>
-
- #define _GetNextEvent 0xA970
-
- static pascal short PatchedGetNextEvent(EventMask, EventRecord *theEvent);
-
- typedef pascal short (*GetNextEventProcPtr)(EventMask, EventRecord *);
- static GetNextEventProcPtr origGetNextEvent;
-
- const char *gLastString;
-
- static short UpdateString(const char *inString)
- {
- long oldA4;
- short result;
-
- oldA4 = SetCurrentA4();
-
- result = strcmp(gLastString, inString) != 0;
-
- gLastString = inString;
-
- SetA4(oldA4);
-
- return result;
- }
-
- static short IsFinderInForeground()
- {
- OSErr err;
- ProcessSerialNumber psn;
- ProcessInfoRec procInfo;
-
- procInfo.processInfoLength = sizeof procInfo;
- procInfo.processName = NULL;
- procInfo.processAppSpec = NULL;
-
- err = GetFrontProcess(&psn);
- if (err) return 0;
-
- err = GetProcessInformation(&psn, &procInfo);
- if (err) return 0;
-
- return (procInfo.processSignature == 'MACS');
- }
-
- static short IsPointingToTrash(Point inPt)
- {
- GrafPtr wMgrPort;
- Rect rect;
-
- GetWMgrPort(&wMgrPort);
- rect = wMgrPort->portRect;
-
- rect.bottom -= 32;
- rect.right -= 32;
- rect.top = rect.bottom - 32;
- rect.left = rect.right - 32;
-
- return PtInRect(inPt, &rect);
- }
-
- static const char *GetGeniusString(EventRecord *theEvent)
- {
- short locationCode;
- WindowPtr wptr;
- const char *geniusMsg = "";
-
- locationCode = FindWindow(theEvent->where, &wptr);
-
- switch (locationCode) {
- case inDesk:
- if (!IsFinderInForeground()) {
- geniusMsg = "Click here to switch to the Finder.";
- } else {
- if (IsPointingToTrash(theEvent->where)) {
- geniusMsg = "This is GeniusBar, a hack by Joshua Juran.";
- } else return NULL;
- }
- break;
- case inMenuBar:
- geniusMsg = "Click here to choose from a menu.";
- break;
- case inDrag:
- geniusMsg = "Click here to move this window.";
- break;
- case inGrow:
- geniusMsg = "Click here to resize this window.";
- break;
- case inGoAway:
- geniusMsg = "Click here to close this window.";
- break;
- case inZoomIn:
- geniusMsg = "Click here to un-zoom this window.";
- break;
- case inZoomOut:
- geniusMsg = "Click here to zoom this window.";
- break;
- case inCollapseBox:
- geniusMsg = "Click here to collapse this window.";
- break;
- case inSysWindow:
- geniusMsg = "This is a system window.";
- case inContent:
- {
- ControlHandle control;
- Point pt = theEvent->where;
- short part;
- Rect rect;
- short vertical;
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(wptr);
- GlobalToLocal(&pt);
- part = FindControl(pt, wptr, &control);
- SetPort(savePort);
- rect = (**control).contrlRect;
- vertical = rect.bottom - rect.top > rect.right - rect.left;
-
- switch (part) {
- case kControlButtonPart:
- geniusMsg = "Click here to push this button.";
- break;
- case kControlCheckBoxPart:
- geniusMsg = "Click here to enable this option.";
- break;
- case kControlUpButtonPart:
- geniusMsg = vertical
- ? "Click here to scroll up."
- : "Click here to scroll left.";
- break;
- case kControlDownButtonPart:
- geniusMsg = vertical
- ? "Click here to scroll down."
- : "Click here to scroll right.";
- break;
- case kControlPageUpPart:
- geniusMsg = vertical
- ? "Click here to scroll up by a page."
- : "Click here to scroll left by a page.";
- break;
- case kControlPageDownPart:
- geniusMsg = vertical
- ? "Click here to scroll down by a page."
- : "Click here to scroll right by a page.";
- break;
- case kControlIndicatorPart:
- geniusMsg = "Click here to scroll to an arbitrary position.";
- break;
- default:
- return NULL;
- break;
- }
- }
- break;
- default:
- return NULL;
- break;
- }
- return geniusMsg;
- }
-
- static DrawGeniusMessage(const char *inString)
- {
- GrafPtr savePort;
- CGrafPtr cWMgrPort;
- short mbarHeight;
- Rect rect;
- RGBColor saveColor, menuColor;
- long len = strlen(inString);
-
- GetPort(&savePort);
- GetCWMgrPort(&cWMgrPort);
- mbarHeight = GetMBarHeight();
-
- rect.top = 1;
- rect.bottom = mbarHeight - 2;
- rect.left = 8;
- rect.right = cWMgrPort->portRect.right - 8;
-
- SetPort((GrafPtr)cWMgrPort);
- GetBackColor(&saveColor);
-
- GetCPixel(5, 5, &menuColor);
- RGBBackColor(&menuColor);
-
- EraseRect(&rect);
-
- MoveTo(8, mbarHeight - 6);
- DrawText(inString, 0, len);
-
- RGBBackColor(&saveColor);
- SetPort(savePort);
- }
-
- static void Payload(EventRecord *theEvent)
- {
- if ((theEvent->modifiers & btnState) == 0) {
- InvalMenuBar();
- } else {
- const char *str = GetGeniusString(theEvent);
- if (UpdateString(str)) {
- if (str == NULL) {
- InvalMenuBar();
- } else {
- DrawGeniusMessage(str);
- }
- }
- }
- }
-
- static pascal short PatchedGetNextEvent(EventMask eventMask, EventRecord *theEvent)
- {
- long oldA4;
- short result;
-
- oldA4 = SetCurrentA4();
-
- result = origGetNextEvent(eventMask, theEvent);
-
- Payload(theEvent);
-
- SetA4(oldA4);
-
- return result;
- }
-
- static ProcPtr ApplyTrapPatch(short trap, ProcPtr patchPtr)
- {
- ProcPtr trapPtr;
-
- if (patchPtr == nil)
- return nil;
-
- trapPtr = NGetTrapAddress (trap, (trap & 0x0800) ? ToolTrap : OSTrap);
- NSetTrapAddress (patchPtr, trap, (trap & 0x0800) ? ToolTrap : OSTrap);
- return trapPtr;
- }
-
- void main(void)
- {
- long oldA4;
- UniversalProcPtr procPtr;
- Handle initCode = nil;
- THz oldZone;
-
- oldA4 = SetCurrentA4();
-
- oldZone = GetZone();
- SetZone(SystemZone());
-
- initCode = (Handle)Get1Resource('INIT', 0);
- if (initCode == nil)
- goto failure;
-
- DetachResource(initCode);
- if (ResError() != noErr)
- goto failure;
-
- HLockHi(initCode);
- HNoPurge(initCode);
-
- procPtr = ApplyTrapPatch( _GetNextEvent, (ProcPtr)(PatchedGetNextEvent) );
- origGetNextEvent = (GetNextEventProcPtr)procPtr;
-
- failure:
- SetZone(oldZone);
- SetA4(oldA4);
- }
-